<?php
/*
Plugin Name: dKret pages
Description: Adds a sidebar widget to display all pages. Subpages can be folded (REQUIRES Fold Page List Plugin)
Author: J. Kretzschmar
Version: 1.0
Author URI: http://diekretzschmars.de
*/

if (!function_exists('wswwpx_fold_page_list'))
	return; 

function dkret_widget_pages( $args ) {
	global $post;
	extract( $args );
	$options = get_option( 'dkret_widget_pages' );

	$title = $options['title'];
	$sortby = empty( $options['sortby'] ) ? 'menu_order' : $options['sortby'];
	$exclude = empty( $options['exclude'] ) ? '' : $options['exclude'];
	$show = empty( $options['exclude'] ) ? '' : $options['show'];
	$fold = empty( $options['exclude'] ) ? '' : $options['fold'];
	

	if ( $sortby == 'menu_order' ) {
		$sortby = 'menu_order, post_title';
	}

	if($post->post_parent)
		$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
	else
		$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");

	if ( ($children) || ($options['show'] == 'checked="checked"') )  {

		echo $before_widget;
		if(!empty($title))
			echo $before_title . $title . $after_title;
?>
		<ul>
			<?php wswwpx_fold_page_list('echo=1&title_li=&sort_column='.$sortby.'&exclude='.$exclude.'',$options['fold']); ?>
		</ul>
<?php
		echo $after_widget;
	}
}

function dkret_widget_pages_control() {
	$options = $newoptions = get_option('dkret_widget_pages');
	if ( $_POST['pages-submit'] ) {
		$newoptions['title'] = strip_tags(stripslashes($_POST['pages-title']));

		$sortby = stripslashes( $_POST['pages-sortby'] );

		if ( in_array( $sortby, array( 'post_title', 'menu_order', 'ID' ) ) ) {
			$newoptions['sortby'] = $sortby;
		} else {
			$newoptions['sortby'] = 'menu_order';
		}

		$newoptions['exclude'] = strip_tags( stripslashes( $_POST['pages-exclude'] ) );
		$newoptions['show'] = isset($_POST['pages-show']);
		$newoptions['fold'] = isset($_POST['pages-fold']);
	}
	if ( $options != $newoptions ) {
		$options = $newoptions;
		update_option('dkret_widget_pages', $options);
	}
	$title = attribute_escape($options['title']);
	$exclude = attribute_escape( $options['exclude'] );
	$show = $options['show'] ? 'checked="checked"' : '';
	$fold = $options['fold'] ? 'checked="checked"' : '';
?>
		<p>
			<label for="pages-title"><?php _e('Title:') ?> <input class="widefat" id="pages-title" name="pages-title" type="text" value="<?php echo $title; ?>" /></label>
			<br />
			<small><?php _e('Leave blank to hide the title.','dkret') ?></small>
		</p>

		<p>
			<label for="pages-show">
				<input class="checkbox" id="pages-show" name="pages-show" type="checkbox" <?php echo $show; ?> />
				<?php _e('Show Navigation on all pages','dkret') ?>
			</label>
			<br />
			<label for="pages-fold">
				<input class="checkbox" id="pages-fold" name="pages-fold" type="checkbox" <?php echo $fold; ?> />
				<?php _e('Show all subpages','dkret') ?>
			</label>
		</p>
		
		<p>
			<label for="pages-sortby"><?php _e('Sort by:'); ?>
				<select name="pages-sortby" id="pages-sortby" class="widefat">
					<option value="post_title"<?php selected( $options['sortby'], 'post_title' ); ?>><?php _e('Page title','dkret'); ?></option>
					<option value="menu_order"<?php selected( $options['sortby'], 'menu_order' ); ?>><?php _e('Page order','dkret'); ?></option>
					<option value="ID"<?php selected( $options['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
				</select>
			</label>
		</p>
		<p>
			<label for="pages-exclude"><?php _e('Exclude:'); ?> <input type="text" value="<?php echo $exclude; ?>" name="pages-exclude" id="pages-exclude" class="widefat" /></label>
			<br />
			<small><?php _e('Page IDs, separated by commas.'); ?></small>
		</p>
		<input type="hidden" id="pages-submit" name="pages-submit" value="1" />
<?php
}

$widget_ops = array('classname' => 'dkret_widget_pages', 'description' => __( "Foldable Sidebarnavigation (dkret)", 'dkret') );
wp_register_sidebar_widget('pages', __('Pages'), 'dkret_widget_pages', $widget_ops);
unregister_widget_control('pages');
wp_register_widget_control('pages', __('Pages'), 'dkret_widget_pages_control');
?>